<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function check_whiteboard_follow($user_id, $follow_id) {
    $query = "SELECT id FROM whiteboard_follows WHERE user_id = '$user_id' AND follow_id = '$follow_id'";
    $follow = safe_select_query($query);
    $query = "SELECT id FROM whiteboard_follows WHERE user_id = '$follow_id' AND follow_id = '$user_id'";
    $followed = safe_select_query($query);

    if($follow) {
        if($followed) {
            return array(
                'guid'          => $follow[0]->id,
                'relationship'  => 'both'
            );
        } else {
            return array(
                'guid'          => $follow[0]->id,
                'relationship'  => 'from'
            );
        }
    } else if ($followed) {
        return array(
                'guid'          => $followed[0]->id,
                'relationship'  => 'to'
            );
    }
    return array(
        'guid'          => -1,
        'relationship'  => 'none'
    );
}

function whiteboard_follow($user_id, $follow_id) {
    if(!$user_id || !$follow_id)
        return FALSE;
    $timestamp = time();
    $query = "INSERT INTO whiteboard_follows(user_id, follow_id, timestamp) VALUES ('$user_id', '$follow_id', '$timestamp')";
    return safe_insert_query($query);
}

function whiteboard_unfollow($user_id, $follow_id) {
    if(!$user_id || !$follow_id)
        return FALSE;
    $query = "DELETE FROM whiteboard_follows WHERE user_id = '$user_id' AND follow_id = '$follow_id'";
    return safe_delete_query($query);
}

function get_whiteboard_following_ids($user_id) {
    $query = "SELECT id, user_id, follow_id, timestamp FROM whiteboard_follows WHERE user_id = '$user_id'";
    return safe_select_query($query);
}

function get_whiteboard_follower_ids($user_id) {
    $query = "SELECT id, user_id, follow_id, timestamp FROM whiteboard_follows WHERE follow_id = '$user_id'";
    return safe_select_query($query);
}

function count_whiteboard_following_ids($user_id) {
    $query = "SELECT count(id) as total FROM whiteboard_follows WHERE user_id = '$user_id'";
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_whiteboard_follower_ids($user_id) {
    $query = "SELECT count(id) as total FROM whiteboard_follows WHERE follow_id = '$user_id'";
    $result = safe_select_query($query);
    return $result[0]->total;
}

function count_whiteboard_mutual_follow_of($user_id) {
    $query = "SELECT count(follow_id) as total
                FROM whiteboard_follows 
                WHERE user_id = '$user_id'
                AND follow_id IN (SELECT user_id FROM whiteboard_follows WHERE follow_id = '$user_id')";
    $result = safe_select_query($query);
    return $result[0]->total;
}

?>
